﻿#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using FunctionalTestsWebsite;
using Grpc.Net.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Tests.FunctionalTests.Helpers;

namespace Grpc.AspNetCore.FunctionalTests.TestServer;

public class FunctionalTestBase
{
    private GrpcChannel? _channel;
    private IDisposable? _testContext;

    protected GrpcTestFixture<Startup> Fixture { get; private set; } = default!;

    protected ILoggerFactory LoggerFactory => Fixture.LoggerFactory;

    protected GrpcChannel Channel => _channel ??= CreateChannel();

    protected GrpcChannel CreateChannel()
    {
        return GrpcChannel.ForAddress(Fixture.Client.BaseAddress!, new GrpcChannelOptions
        {
            LoggerFactory = LoggerFactory,
            HttpClient = Fixture.Client
        });
    }

    protected virtual void ConfigureServices(IServiceCollection services)
    {
    }

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        Fixture = new GrpcTestFixture<Startup>(ConfigureServices);
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        Fixture.Dispose();
    }

    [SetUp]
    public void SetUp()
    {
        _testContext = Fixture.GetTestContext();
    }

    [TearDown]
    public void TearDown()
    {
        _testContext?.Dispose();
        _channel = null;
    }
}